package com.testmanager.testmananger.shuiyou.strCompare.respository;


import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.testmanager.testmananger.shuiyou.strCompare.domain.QSyCompareTaskEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.QSySqlCompareScriptEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyCompareTaskEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.CompareTasksSearchDto;
import com.testmanager.testmananger.shuiyou.strCompare.service.SyProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;

@Repository
@Transactional(readOnly = true)
@Slf4j
public class SyCompareTaskCustom {

    @Autowired
    SyCompareTaskRepository syCompareTaskRepository;

    @Autowired
    SyProjectService syProjectService;


    public Page<SyCompareTaskEntity> search(CompareTasksSearchDto sqlCompareTasksSearchDto){
        log.info("SyCompareTaskCustom:{}",sqlCompareTasksSearchDto);

       //QSyCompareTaskEntity
        QSyCompareTaskEntity qSyCompareTaskEntity = QSyCompareTaskEntity.syCompareTaskEntity;
        BooleanExpression expression = qSyCompareTaskEntity.id.isNotNull();


        if (sqlCompareTasksSearchDto.getProjectId() != 0){
           // expression = expression.and(qSyCompareTaskEntity.projectId.eq(sqlCompareTasksSearchDto.getProjectId()));
            expression = expression.and(qSyCompareTaskEntity.projectId.in(syProjectService.findAllChildRenIds(sqlCompareTasksSearchDto.getProjectId())));
        }
        if (sqlCompareTasksSearchDto.getResultStatus() != 0){
           // expression = expression.and(qSyCompareTaskEntity.projectId.eq(sqlCompareTasksSearchDto.getProjectId()));
            expression = expression.and(qSyCompareTaskEntity.result.eq(sqlCompareTasksSearchDto.getResultStatus()));
        }
        if (sqlCompareTasksSearchDto.getTaskName() != null && sqlCompareTasksSearchDto.getTaskName() != "" &&!StringUtils.isEmpty(sqlCompareTasksSearchDto.getTaskName())){
            expression=expression.and(qSyCompareTaskEntity.name.like("%"+sqlCompareTasksSearchDto.getTaskName()+"%"));
        }
        if (sqlCompareTasksSearchDto.getScriptName() != null && sqlCompareTasksSearchDto.getScriptName() != "" &&!StringUtils.isEmpty(sqlCompareTasksSearchDto.getScriptName())){
            expression=expression.and(qSyCompareTaskEntity.scriptName.like("%"+sqlCompareTasksSearchDto.getScriptName()+"%"));
        }
        if (sqlCompareTasksSearchDto.getCreateBy() != null && sqlCompareTasksSearchDto.getCreateBy() != "" &&!StringUtils.isEmpty(sqlCompareTasksSearchDto.getCreateBy())){
            expression=expression.and(qSyCompareTaskEntity.createBy.like("%"+sqlCompareTasksSearchDto.getCreateBy()+"%"));
        }
       // expression.and(QSySqlCompareScriptEntity.sySqlCompareScriptEntity.name.like(""));

            expression=expression.and(qSyCompareTaskEntity.status.eq(1));

        Sort orders = Sort.by(Sort.Order.desc(qSyCompareTaskEntity.lastModifiedDate.getMetadata().getName()));

        Pageable pageable = PageRequest.of(sqlCompareTasksSearchDto.getPageNum()-1,sqlCompareTasksSearchDto.getPageSize(),orders);

        return syCompareTaskRepository.findAll(expression,pageable);

    }







}
